package apps.reading.ws.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import apps.reading.ParserAction;
import apps.reading.db.WebSourceBean;
import apps.reading.db.WebSourceService;
import core.tool.json.JsonHelper;
import core.tool.json.JsonHelperFactory;
import core.tool.log.log4j.EasyLogger;
import core.web.easyui.TreeNode;

@Controller
@RequestMapping("/wslist")
public class WebSourceController {
	
    @Autowired
	private WebSourceService webSourceService;
	
	@RequestMapping("/list")
	public ModelAndView initList() {
		ModelAndView mav = new ModelAndView();
		Map<String,String> sourceTypeMap=WebSourceBean.sourceTypeMap;
		Iterator<Entry<String, String>> entryKeyIterator = sourceTypeMap.entrySet().iterator(); 
		List<TreeNode> list=new ArrayList<TreeNode>();
		while (entryKeyIterator.hasNext()) {     
		    Entry<String, String> e = entryKeyIterator.next();     
		    String key=e.getKey();  
		    String value=e.getValue();  
		    TreeNode treeNode=new TreeNode(key,value);
		    list.add(treeNode);
		}  	
		JsonHelper jsonHelper=JsonHelperFactory.getInstanse();
		String result=jsonHelper.getJsonStr(list);
		mav.addObject("treeNodes", result);
		mav.setViewName("reading_client/wslist");
		return mav;
	}
	
	
	@RequestMapping("/getPagerList")
	public @ResponseBody Object getPagerList(@RequestParam("sourceType")String sourceType,@RequestParam("page")int page,@RequestParam("size")int size) {
		EasyLogger.debug("页码："+page+"记录条数："+size);
		page=page-1;
		Sort sort=new Sort(Direction.DESC,"publicDate");
		PageRequest pageReqeust=new PageRequest(page,size,sort);
		ModelAndView mav = new ModelAndView();
		Page<WebSourceBean> pageResult=webSourceService.findBySourceTypePager(sourceType, pageReqeust);
        Map attributes = new HashMap();
        attributes.put("total", pageResult.getTotalElements());
        attributes.put("rows", pageResult.getContent());
		return attributes;
	}	
	
	
	@RequestMapping("/read")
	public @ResponseBody Object read() {
		ParserAction parserAction=new ParserAction();
		try {
			parserAction.parser();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "<script>alert('success!')</script>";
	}	
	
	public static void main(String[] args) {
		
		
	}
	
}
